<?php

/*
*/

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Supplier extends CI_Controller {

	public $data;
	private $access_id = 100;
	private $limit = 15;

	public function __construct()
	{
		parent::__construct();
		//载入必要类文件
		$this->load->model('Login_model');
		$this->load->model('User_model');
		$this->load->model('Right_model');
		$this->load->model('Supplier_model');
		$this->load->model('Goods_type_model');
		$this->load->model('Main_goods_model');
		$this->load->model('Sub_goods_model');
		//载入必要库文件
		$this->load->library('Access_control');
		$this->load->library('Menu_control');
		$this->load->library('Forbid_resubmit');
		//载入配置文件 并 初始化控制器
		$this->data['site'] = $this->config->item('site');
		$this->data['title'] = '供应商管理';
		$this->data['styles'] = array('master.css', 'error.css', 'supplier.css');
		$this->data['scripts'] = array('master.js', 'supplier.js');
		//检查是否是登录用户
		if (!$this->Login_model->check(TRUE))
		{
			header("Location: ".$this->data['site']['root'].'/login');
		}
		//设置站点基本参数
		$this->data['cur_user'] = $this->User_model->getUser($this->input->cookie('user_id'));
		$this->data['header']['username'] = $this->input->cookie('realname');
		$this->data['site']['environment'] = $this->User_model->get_envrionment();
		$this->data['menus'] = $this->config->item('menus');
		$this->data['menus'] = $this->menu_control->main($this->data['menus'], 5);
		$this->data['sub_menus'] = $this->_menus();
		$this->data['sub_menu_index'] = '-1';
	}

	//获得子菜单
	private function _menus()
	{
		return array(
			array(
				'index'	=>	1,
				'url'	=>	$this->data['site']['root'] . 'supplier/all/',
				'name'	=>	'所有供应商',
				'right' => 'supplier.all'
			),
			array(
				'index'	=>	2,
				'url'	=>	$this->data['site']['root'] . 'supplier/create/',
				'name'	=>	'新增供应商',
				'right' => 'supplier.create'
			),
			/***
			array(
				'index' =>	3,
				//'url'	=>	$this->data['site']['root'] . 'supplier/search',
				'url'	=>	'javascript:void(0)',
				'name'	=>	'搜索[开发中]'
				)
***/
		);
	}

	//优先级菜单
	private function _priority()
	{
		return array(
			array(
				'id'	=>	1,
				'name'	=>	'最高'
			),
			array(
				'id'	=>	2,
				'name'	=>	'普通'
			),
			array(
				'id'	=>	3,
				'name'	=>	'最低'
			)
		);
	}

	public function index()
	{
		$this->all();
	}

	public function create()
	{
		$this->data['sub_menu_index'] = 2;
		//初始化表单信息
		$this->data['form'] = array();
		$this->data['priority'] = $this->_priority();
		//判断提交以及重复提交
		if ($this->forbid_resubmit->judge_submit($this->input->post('rand')))
		{
			//判断新增是否成功
			if ($supplier_id = $this->Supplier_model->create())
			{
				header('Location: ' . $this->data['site']['root'] . 'supplier/update/' . $supplier_id);
			}
			$this->data['form']['result'] = '提交出错，请重试';
		}
		$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
		$this->load->view_('supplier_create_view', $this->data);
	}

	public function update($id, $form=false)
	{
		//添加菜单
		array_unshift($this->data['sub_menus'], array('index'=>101,'url'=>'javascript:void(0)','name'=>'更新供应商'));
		$this->data['sub_menu_index'] = 101;
		//初始化表单信息
		$this->data['form'] = array();
		$this->data['priority'] = $this->_priority();
		//判断是否供应商是否存在
		//获得最新的表单信息
		if (!$this->data['form'] = $this->Supplier_model->getOne($id))
		{
			$this->data['error'] = '访问错误，返回重试';
			$this->load->view_('error_500_view', $this->data);
		}
		if ($this->forbid_resubmit->judge_submit($this->input->post('rand')))
		{
			//注销可编辑的表单值
			unset($this->data['form']['supplier_name'], $this->data['form']['supplier_contact'], $this->data['form']['supplier_loaction']);
			//判断是否更新成功
			if ($this->Supplier_model->update())
			{
				$this->data['form']['result'] = '更新成功';
			}
		}
		$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
		$this->load->view_('supplier_update_view', $this->data);
	}

	public function all()
	{
		$this->data['sub_menu_index'] = 1;
		//格式化优先级数据
		$this->data['priority'] = array();
		foreach ($this->_priority() as $p)
		{
			$this->data['priority'][$p['id']] = $p['name'];
		}
		$this->data['suppliers'] = $this->Supplier_model->getAll();
		$this->load->view_('supplier_list_view', $this->data);
	}

	public function del()
	{
		$supplier_ids = $this->input->post('supplier_id');
		foreach ($supplier_ids as $id)
		{
			$this->Supplier_model->del($id);
		}
		$this->data['ajax_request'] = 'DEL_SUCCESS';
		$this->load->view('ajax_request', $this->data);
	}

	public function recover()
	{
		$supplier_ids = $this->input->post('supplier_id');
		foreach ($supplier_ids as $id)
		{
			$this->Supplier_model->recover($id);
		}
		$this->data['ajax_request'] = 'RECOVER_SUCCESS';
		$this->load->view('ajax_request', $this->data);
	}

}